package net.jueb.gameServer;

import java.util.concurrent.atomic.AtomicInteger;

import net.jueb.util4j.common.game.AbstractService;
import net.jueb.util4j.queue.queueExecutor.queueGroup.QueueGroupExecutor;
import net.jueb.util4j.queue.queueExecutor.queueGroup.impl.DefaultQueueGroupExecutor;

public class GameService extends AbstractService{
	
	private GameService() {
	}
	private static GameService instance;
	public static GameService getInstance()
	{
		if(instance==null)
		{
			synchronized (GameService.class) {
				if(instance==null)
				{
					instance=new GameService();
				}
			}
		}
		return instance;
	}
	ScriptFactory sf=null;
	QueueGroupExecutor qes=new DefaultQueueGroupExecutor();
	public AtomicInteger StaticInt=new AtomicInteger(0);
	public AtomicInteger NormalInt=new AtomicInteger(0);
	public static final int StaticScriptCode=1001;
	public static final int NormalScriptCode=1002;
	short queue1=1;
	short queue2=2;
	short queue3=3;
	@Override
	protected void doStart() throws Throwable {
		sf=new ScriptFactory("D:/scriptLib/",true);//设置脚本目录和自动重载
		qes.execute(queue1, new TestTask());//启动一个循环阻塞的任务,此任务占用队列1
	}

	/**
	 * 执行测试任务,此任务阻塞
	 * @author jaci
	 */
	class TestTask implements Runnable{
		@Override
		public void run() {
			for(;;)
			{
				try {
					Thread.sleep(5000);//5秒执行一次1001和1002脚本
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
				//取脚本并放入队列执行
				final IGameScript script1=sf.buildInstance(StaticScriptCode);
				final IGameScript script2=sf.buildInstance(NormalScriptCode);
				if(script1!=null)
				{
					qes.execute(queue2, new Runnable() {
						@Override
						public void run() {
							script1.run();
						}
					});
				}
				if(script2!=null)
				{
					qes.execute(queue3, new Runnable() {
						@Override
						public void run() {
							script2.run();
						}
					});
				}
			}
		}
	}
	
	@Override
	protected void doClose() throws Throwable {
		// TODO Auto-generated method stub
		
	}
}
